package edu.njust.LabSystem.Controller;

import edu.njust.LabSystem.Service.LabAppointmentService;
import edu.njust.LabSystem.Service.StudentAppointmentService;
import edu.njust.LabSystem.entity.LabAppointment;
import edu.njust.LabSystem.entity.StudentAppointment;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class StudentAppointmentController extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public StudentAppointmentController() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//System.out.println("进来了");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		HttpSession session = request.getSession();
		
		String seatId = request.getParameter("seatId");//座位号
		String eid = (String)session.getAttribute("eid");//实验id
		String uid=(String)session.getAttribute("uid");
			
		session.setAttribute("seatId", seatId);
		StudentAppointment sAppointment=new StudentAppointment();
		sAppointment.setSeat(seatId);
//		System.out.println("seat:"+seatId);
		
		String labId=(String)session.getAttribute("labId");//实验室ID
//		System.out.println("student:"+uid);
//		System.out.println("experiment:"+slid);
//		System.out.println("lab:"+labId);
//
		LabAppointmentService lAService=new LabAppointmentService();
		LabAppointment labAppointment=lAService.getLabAppointmentByExperiment(eid);

		java.sql.Date appoint_date=labAppointment.getAppoint_date();

		int appoint_time=labAppointment.getAppoint_time();		
		
//		SeatService seatService=new SeatService();
//		List<Seat> seats=seatService.getSeatsById(labId);
//		for(Seat seat:seats){
//			if(seat.getSeat().equals(seatId)&&seat.getIsTaken()==false){
//				seat.setIsTaken(true);
//				break;
//			}
//			else{
//				response.getWriter().println("<script> alert(\"该位置已经被占用\");window.location.href='SelectLab.jsp';</script>");
//				break;
//			}
//		}
		
		StudentAppointmentService sService=new StudentAppointmentService();
		List<StudentAppointment> sAppointments=sService.getStudentAppointmentByExperiment(eid);
		//System.out.println("集合："+sAppointments);

		if(sAppointments==null||sAppointments.size()<1){
			sAppointment.setStudent(uid);
			sAppointment.setExperiment(eid);
			sAppointment.setLab(labId);
			sAppointment.setAppoint_date(appoint_date);
			sAppointment.setAppoint_time(appoint_time);
			System.out.println(sAppointment);
			
			sService.insertStudentAppointment(sAppointment);
			request.getRequestDispatcher("/StudentHomePage.jsp").forward(request, response);
		}else{			
		for(StudentAppointment sa:sAppointments){
			if(seatId.equals(sa.getSeat())){
				//System.out.println("1sdfdsfjdshfs");
				response.getWriter().println("<script > alert(\"Seat occupied!\");window.history.back(-1);</script>");
				eid = (String)session.getAttribute("eid");
				//request.getRequestDispatcher("SelectLab.jsp").forward(request, response);
				break;
			}else{
				sAppointment.setStudent(uid);
				sAppointment.setExperiment(eid);
				sAppointment.setLab(labId);
				sAppointment.setAppoint_date(appoint_date);
				sAppointment.setAppoint_time(appoint_time);
				System.out.println(sAppointment);
				
				sService.insertStudentAppointment(sAppointment);

				request.getRequestDispatcher("/StudentHomePage.jsp").forward(request, response);
				
			}
		}
		}

		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
